package cn.com.ddapp.ddm.base;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.zhy.autolayout.AutoLayoutActivity;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.bean.mine.UserData;
import cn.com.ddapp.ddm.ui.dialog.DialogControl;
import cn.com.ddapp.ddm.ui.dialog.DialogHelper;
import cn.com.ddapp.ddm.ui.dialog.WaitDialog;
import cn.com.ddapp.ddm.ui.mine.PersonCenterActivity;
import cn.com.ddapp.ddm.util.TDevice;


/**
 * Created by renlei on 2016/5/23.
 */
public abstract class BaseActivity extends AutoLayoutActivity implements DialogControl,View.OnClickListener {

	public static final int NICKNAME_OK = 0;
	public static final int SEX_OK = 1;
	public static final int BIRTH_OK = 2;

	protected Gson gson = new Gson();

	Unbinder unbinder;

	private TextView titleView;
	private ImageView rightImageView;
	private TextView rightTextView;
	private ImageView leftImageView;
	private boolean _isVisible = true;
	private WaitDialog _waitDialog;
	protected  Context mContext;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if(TDevice.isTablet(getApplicationContext())){
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 横屏
		}else{
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
		}

		setContentView(getContentViewId());
		mContext=this;
		unbinder = ButterKnife.bind(this);
		ActivityManager.getInstance().addActivity(this);

		initView();
		initData();

	}

	public abstract void initView();

	public abstract void initData();

	public abstract int getContentViewId();

	@Override
	public void onClick(View view) {

	}

	public void setTitle(String text) {
		titleView = (TextView) findViewById(R.id.titletext);
		titleView.setText(text);
		titleView.setVisibility(View.VISIBLE);
	}

	public void setTitle(int id) {
		titleView = (TextView) findViewById(R.id.titletext);
		titleView.setText(getResources().getText(id));
		titleView.setVisibility(View.VISIBLE);
	}

	public void setTitle(int id, int color) {
		titleView = (TextView) findViewById(R.id.titletext);
		titleView.setText(getResources().getText(id));
		titleView.setTextColor(color);
		titleView.setVisibility(View.VISIBLE);
	}

	public void setTitleLeftImg(int resid) {
		leftImageView = (ImageView) findViewById(R.id.back);
		leftImageView.setImageResource(resid);
		leftImageView.setVisibility(View.VISIBLE);
		leftImageView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
	}

	public void setTitleLeftImg(int resid, View.OnClickListener clickListener) {
		leftImageView = (ImageView) findViewById(R.id.back);
		leftImageView.setImageResource(resid);
		leftImageView.setVisibility(View.VISIBLE);
		leftImageView.setOnClickListener(clickListener);
	}

	public void setTitleRightImg(int resid, View.OnClickListener clickListener) {
		rightImageView = (ImageView) findViewById(R.id.right_btn);
		rightImageView.setImageResource(resid);
		rightImageView.setVisibility(View.VISIBLE);
		rightImageView.setOnClickListener(clickListener);
	}

	public void setTitleRightText(int resid, View.OnClickListener clickListener) {
		rightTextView = (TextView) findViewById(R.id.right_text);
		rightTextView.setText(resid);
		rightTextView.setVisibility(View.VISIBLE);
		rightTextView.setOnClickListener(clickListener);
	}
	public void setTitleRightTv(String resid, View.OnClickListener clickListener) {
		rightTextView = (TextView) findViewById(R.id.right_text);
		rightTextView.setText(resid);
		rightTextView.setVisibility(View.VISIBLE);
		rightTextView.setOnClickListener(clickListener);
	}
	public TextView getTitleView() {
		return titleView;
	}

	public TextView getRightTextView() {
		return rightTextView;
	}

	public ImageView getRightImageView() {
		return rightImageView;
	}

	public ImageView getLeftImageView() {
		return leftImageView;
	}


	@Override
	public void onDestroy() {
		super.onDestroy();
		unbinder.unbind();
		OkHttpUtils.getInstance().cancelTag(this);
		ActivityManager.getInstance().finishActivity(this);
	}

	@Override
	public WaitDialog showWaitDialog() {
		return showWaitDialog(R.string.loading);
	}

	@Override
	public WaitDialog showWaitDialog(int resid) {
		return showWaitDialog(getString(resid));
	}

	@Override
	public WaitDialog showWaitDialog(String message) {
		try {
			if (_waitDialog == null) {
				_waitDialog = DialogHelper.getWaitDialog(this, message);
			}
			if (_waitDialog != null) {
				_waitDialog.setMessage(message);
//                _waitDialog.show();
				WaitDialog.show(_waitDialog);
			}
			return _waitDialog;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public void hideWaitDialog() {
		if (_isVisible && _waitDialog != null) {
			try {
				_waitDialog.dismiss();
				_waitDialog = null;
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

	protected abstract String getFragmentTag();

	protected void cropImage(Uri data) {
		Intent intent = new Intent();
		intent.setAction("com.android.camera.action.CROP");
		intent.setDataAndType(data, "image/*");// mUri是已经选择的图片Uri
		intent.putExtra("crop", "true");// 才能出剪辑的小方框，不然没有剪辑功能，只能选取图片
		//裁剪框比例
//		intent.putExtra("aspectX", 360);
//		intent.putExtra("aspectY", 350);
		//裁剪框比例
		intent.putExtra("aspectX",1);
		intent.putExtra("aspectY",1);
		//图片输出大小
		intent.putExtra("outputX", 200);// 输出图片大小
		intent.putExtra("outputY", 200);
		intent.putExtra("return-data", true);

		startActivityForResult(intent, PersonCenterActivity.CROP_PICTURE);
		Log.i("YU","--dat1a--"+data);
	}

	protected File getTempImage() {
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			///storage/emulated
			File imgDirectory = new File(Environment.getExternalStorageDirectory(), "ezhi");
			Log.e("111",Environment.getExternalStorageDirectory()+"");

			if (!imgDirectory.exists()) {
				imgDirectory.mkdirs(); // 创建一个selectPhoto的文件夹
			}

			File tempImgFile = new File(imgDirectory, "headPic.jpg");
			try {
				tempImgFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return tempImgFile;
		}

		return null;
	}

	protected UserData getUser(){
		return null;
	}

}
